<?php

namespace App\Exports;

use Maatwebsite\Excel\Concerns\FromArray;
use PhpOffice\PhpSpreadsheet\Shared\Date;            // 日期处理
use PhpOffice\PhpSpreadsheet\Style\NumberFormat;    // 数字格式化
use PhpOffice\PhpSpreadsheet\Cell\DataType;
use PhpOffice\PhpSpreadsheet\Cell\StringValueBinder;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Concerns\WithColumnFormatting;
use Maatwebsite\Excel\Concerns\WithCustomValueBinder;
use Maatwebsite\Excel\Concerns\FromQuery;

use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Events\BeforeExport;
use Maatwebsite\Excel\Events\BeforeWriting;
use Maatwebsite\Excel\Events\BeforeSheet;
use Maatwebsite\Excel\Events\AfterSheet;

class PublishSheet extends StringValueBinder implements FromArray,WithHeadings,
                                            WithColumnFormatting, WithCustomValueBinder, WithEvents
{
    private $students;

    public function __construct($students)
    {
        $this->students = $students;
    }

    // 查询
    public function array():array
    {
        $result = [];

        foreach($this->students as $student){
            $result[] = [
                $student->account,
                $student->info->name,
                $student->info->mobile,
                $student->info->idcard
            ];
        }

        return $result;

    }


    //格式化
    public function registerEvents(): array
    {
        return [
            // Handle by a closure.
            /*BeforeExport::class => function(BeforeExport $event) {
                $event->writer->getProperties()->setCreator('Patrick');
            },

            // Array callable, refering to a static method.
            BeforeWriting::class => [self::class, 'beforeWriting'],*/

            // Using a class with an __invoke method.
            AfterSheet::class => [self::class, 'AfterSheetHandler']
        ];
    }


    public static function AfterSheetHandler(AfterSheet $event)
    {
        // 所有表头-设置字体为14
        $cellRange = 'A1:F1';
        //$event->sheet->getDelegate()->getStyle($cellRange)->getFont()->setSize(14);

        // 将第一行行高设置为20
        $event->sheet->getDelegate()->getDefaultRowDimension()->setRowHeight(20);

        //设置列宽
        $event->sheet->getDelegate()->getColumnDimension('B')->setWidth(11);
        $event->sheet->getDelegate()->getColumnDimension('C')->setWidth(15);
        $event->sheet->getDelegate()->getColumnDimension('D')->setWidth(25);

        //设置区域单元格垂直居中
        $event->sheet->getDelegate()->getParent()->getDefaultStyle()->getAlignment()->setVertical('center');
        $event->sheet->getDelegate()->getParent()->getDefaultStyle()
            ->applyFromArray([
                'font' => [
                    'name' => 'Microsoft Yahei',
                    'bold' => false,
                    'italic' => false,
                    'strikethrough' => false,
                    'color' => [
                        'rgb' => '000000'
                    ]
                ]

            ]);

    }


    public function columnFormats(): array
    {
        return [
            'C' => NumberFormat::FORMAT_GENERAL,
            'D' => NumberFormat::FORMAT_GENERAL,
        ];
    }

    public function headings(): array
    {
        return [
            '账号', '姓名', '手机', '证件号码'
        ];
    }
}
